package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    //引用service
    private CheckGroupService checkGroupService;

    //设置权限
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_ADD')")
    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        //try
        try {
            //调用add方法
            checkGroupService.add(checkGroup, checkitemIds);
            //返回添加成功
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            //返回添加失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }
    //设置修改权限
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findPage")
    //分页查询
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用分页查询方法
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        //返回PageResult
        return pageResult;
    }

    //添加编辑权限
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_DEIT')")
    @RequestMapping("/edit")
    //编辑
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        //try
        try {
            //调用编辑方法
            checkGroupService.edit(checkGroup, checkitemIds);
            //返回编辑成功
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            //返回编辑失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }


    @RequestMapping("/findById")
    //通过id查询
    public Result findById(Integer id) {
        //调用id查询方法
        CheckGroup checkGroup = checkGroupService.findById(id);
        //判断数据是否为空
        if (checkGroup != null) {
            //数据不为空新建查询成功Result
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            //result调用setData方法吧查询结果添加进result
            result.setData(checkGroup);
            //返回result
            return result;
        } else {
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    //通过检查组id查询检查项
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        //try
        try {
            //调用查询方法返回检查项id集合
            List<Integer> checkitemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            //返回查询成功带检查项id集合
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PreAuthorize("hasAnyAuthority('CHECKGROUP_DELETE')")
    @RequestMapping("/delete")
    //删除
    public Result delete(Integer id) {
        //try
        try {
            //调用删除方法
            checkGroupService.delete(id);
        } catch (RuntimeException e) {
            //catch自定义错误并返回
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            //返回删除失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        //返回删除成功
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
    @RequestMapping("/findAll")
    //查询所有
    public Result findAll() {
        //调用查询所有方法返回检查组集合
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        //判断检查组集合是否为空
        if (checkGroupList != null && checkGroupList.size() > 0) {
            //不为空新建Result 传入查询成功和查询结果集合
            Result result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
            //将查询结果添加进Result
            result.setData(checkGroupList);
            //返回result
            return result;
        }
        //返回查询失败
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
}
